package io.jft.doll.model.viewModel

import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.MutableLiveData
import com.g.base.extend.resultNotNull
import com.g.base.extend.switchMap
import com.g.base.extend.toObservable
import com.g.base.room.repository.ResultWarp
import com.g.base.viewModel.BaseViewModel
import com.google.gson.JsonObject
import io.jft.doll.room.entity.remote.SuggestChargeREntity
import io.jft.doll.room.repository.RoomRepository
import io.jft.doll.room.repository.UserRepository
import io.reactivex.Observable
import io.reactivex.functions.BiFunction

/**
 * Created by G on 2017/11/13 0013.
 */
class UserViewModel : BaseViewModel() {
    private val userRepository by lazy { UserRepository() }
    private val roomRepository by lazy { RoomRepository() }
    //用户余额
    private val remainingSumOperate by lazy { MutableLiveData<Boolean>() }
    //房间详情
    private val roomDetailOperate by lazy { MutableLiveData<String>() }
    //邀请信息
    private val inviteInfoOperate by lazy { MutableLiveData<Boolean>() }

    fun operateRoomDetail(roomId: String) {
        roomDetailOperate.value = roomId
    }

    fun operateRemain(force: Boolean = true) {
        remainingSumOperate.value = force
    }

    fun changeRemain(remain: String) {
        UserRepository.currentRemain.postValue(remain)
    }

    fun obsRemainingSum() = remainingSumOperate.switchMap { userRepository.getRemainingSum(it) }

    fun obsRoomDetail() = roomDetailOperate.switchMap { roomRepository.getRoomDetail(it) }

    /**
     * 用户余额与建议充值
     */
    fun getSuggestChargeRemainingSumZip(owner: LifecycleOwner): Observable<Pair<ResultWarp<String?>, ResultWarp<SuggestChargeREntity?>>> {
        val remainSum = userRepository.getRemainingSum(true).resultNotNull().toObservable(owner)
        val suggestCharge = userRepository.getSuggestCharge().resultNotNull().toObservable(owner)
        return Observable.combineLatest(remainSum, suggestCharge, BiFunction { resultWarp: ResultWarp<String?>, resultWarp1: ResultWarp<SuggestChargeREntity?> ->
            Pair(resultWarp, resultWarp1)
        })
    }

    /**
     * 用户账单
     */
    val billsLimit = 20
    private val billsOperate by lazy { MutableLiveData<JsonObject>() }

    fun operateBills(type: String, offset: Int) {
        billsOperate.value = JsonObject().apply {
            addProperty("type", type)
            addProperty("offset", offset.toString())
            addProperty("limit", billsLimit.toString())
        }
    }

    fun obsBillsHistory() = billsOperate.switchMap { userRepository.getBillsHistory(it) }

    fun makeCharge(amount: String, type: String) = userRepository.makeCharge(
            JsonObject().apply {
                addProperty("amount", amount)
                addProperty("type", type)
            })

    fun asyncChargeStatus(id: String) = userRepository.asyncChargeStatus(id)

    fun getDuiBarAddress() = userRepository.getDuiBarAddress()

    fun operateInviteInfo() {
        inviteInfoOperate.value = true
    }

    fun obsInviteInfo() = inviteInfoOperate.switchMap { userRepository.getInviteInfo() }

    fun inputInviteCode(code: String) = userRepository.inputInviteCode(JsonObject().apply { addProperty("code", code) })
}